
package com.insigma.business.qrcode.welfare._bff.impl;


import cn.hutool.core.bean.BeanUtil;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.SimpleApiRequest;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.welfare.dto.HospitalStatsDTO;
import com.insigma.business.qrcode.welfare.dto.ScanCodeRecordDTO;
import com.insigma.business.qrcode.welfare.dto.ScenicSpotStatsDTO;
import com.insigma.business.qrcode.welfare.request.ScanCodeRecordInsertRequest;
import com.insigma.business.qrcode.welfare.request.ScanCodeRecordSearchRequest;
import com.insigma.business.qrcode.welfare.request.ScanCodeRecordUpdateRequest;
import com.insigma.business.qrcode.welfare._bff.ScanCodeRecordBFFProvider;
import com.insigma.business.qrcode.welfare._consumer.ScanCodeRecordConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("scanCodeRecordBFFProvider")
public class ScanCodeRecordBFFProviderImpl extends BaseProviderImpl implements ScanCodeRecordBFFProvider {

    @Autowired
    private ScanCodeRecordConsumer scanCodeRecordConsumer;


    @Override
    public BasePagingResponse<List<ScanCodeRecordDTO>> searchScanCodeRecord(final ScanCodeRecordSearchRequest searchRequest) {

        final BasePagingResponse<List<ScanCodeRecordDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页ScanCodeRecord的请求参数不能为空");
            }

            @Override
            public void executeService() {
                searchRequest.setSortKeyValueJoint("createDatetime_desc");
                BasePagingResponse<List<ScanCodeRecordDTO>> listBasePagingResponse = scanCodeRecordConsumer.searchScanCodeRecord(searchRequest);
                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SCAN_CODE_RECORD_SEARCH;
            }
        });

        return basePagingResponse;
    }

    @Override
    public BasePagingResponse<List<ScanCodeRecordDTO>> searchHospitalRecord(ScanCodeRecordSearchRequest searchRequest) {

        return scanCodeRecordConsumer.searchHospitalRecord(searchRequest);
    }



    @Override
    public BaseResponse<ScanCodeRecordDTO> getScanCodeRecord(final BaseGetRequest baseGetRequest) {

        final BaseResponse<ScanCodeRecordDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条ScanCodeRecord的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<ScanCodeRecordDTO> scanCodeRecordResponse = scanCodeRecordConsumer.getScanCodeRecord(baseGetRequest);
                BeanUtil.copyProperties(scanCodeRecordResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SCAN_CODE_RECORD_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertScanCodeRecord(final ScanCodeRecordInsertRequest insertRequest) {

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增ScanCodeRecord的请求参数不能为空");
            }

            @Override
            public void executeService() {
                BaseResponse<String> scanCodeRecordResponse = scanCodeRecordConsumer.insertScanCodeRecord(insertRequest);
                BeanUtil.copyProperties(scanCodeRecordResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SCAN_CODE_RECORD_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateScanCodeRecord(final ScanCodeRecordUpdateRequest updateRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改ScanCodeRecord的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(), "操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(), "操作人类型不能为空");
            }

            @Override
            public void executeService() {
                BaseResponse<Integer> scanCodeRecordResponse = scanCodeRecordConsumer.updateScanCodeRecord(updateRequest);
                BeanUtil.copyProperties(scanCodeRecordResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SCAN_CODE_RECORD_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteScanCodeRecord(final BaseDeleteRequest deleteRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除ScanCodeRecord的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getModifierId(), "操作人不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(), "操作人类型不能为空");
            }

            @Override
            public void executeService() {
                BaseResponse<Integer> scanCodeRecordResponse = scanCodeRecordConsumer.deleteScanCodeRecord(deleteRequest);
                BeanUtil.copyProperties(scanCodeRecordResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SCAN_CODE_RECORD_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<ScenicSpotStatsDTO> getScenicSpotStats(SimpleApiRequest request) {

        return scanCodeRecordConsumer.getScenicSpotStats(request);
    }

    @Override
    public BaseResponse<HospitalStatsDTO> getHospitalStats(SimpleApiRequest request) {

        return scanCodeRecordConsumer.getHospitalStats(request);
    }

}
